<?php

/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 * 
 */

/**
 * Command object for a SharePointListQuery. This command object executes the 
 * ExecuteCamelotXML method in the Camelot SharePoint Integration Service
 * @author Bendsoft
 */
class SharePointListCommand extends SharePointCommandBase {

    /**
     * Select a specific list (not used if the sql property is set)
     * @var string
     */
    public $listName;

    /**
     * Select a specific view (if not set we'll fetch the default view)
     * @var string
     */
    public $viewName;

    /**
     * (Optional) Columns to fetch from the view
     * @var string|array Semicolon separated string or array
     */
    public $columns;

    /**
     * 
     * @param string $connection_name Name of the connection
     * @param string $listName Select a specific list (not used if the sql property is set)
     * @param string $viewName Select a specific view (if not set we'll fetch the default view)
     * @param string|array $columns Semicolon separated string or array
     * @param bool $includeAttachments Currently not implemented (default false)
     */
    public function __construct($connection_name = null, $listName = null, $viewName = null, $columns = null, $includeAttachments = null) {
        parent::__construct();
        $this->connection_name = $connection_name;
        $this->listName = $listName;
        $this->viewName = $viewName;
        $this->columns = $columns;
        $this->includeAttachments = $includeAttachments;
    }

    public function buildCommandFromArray($args) {
        if (CamelotExtensions::getVariableType($args) == "array") {
            $this->connection_name = array_key_exists('connection_name', $args) ? $args['connection_name'] : '';
            $this->listName = array_key_exists('listName', $args) ? $args['listName'] : '';
            $this->viewName = array_key_exists('viewName', $args) ? $args['viewName'] : '';
            $this->includeAttachments = array_key_exists('includeAttachments', $args) ? $args['includeAttachments'] : '';
        }
    }

    /**
     * Validate a SharePointListCommand
     * @param SharePointListCommand $command
     * @return CamelotException|boolean
     */
    public function isValid(SharePointListCommand $command = null) {
        // Set $command as the current instace if $command is null and the instance is set
        if ($command == null && isset($this)) {
            $command = $this;
        }

        // Validate the connection name
        if (empty($command->connection_name)) {
            return new CamelotException(null, 406);
        }

        if (empty($command->listName)) {
            return new CamelotException(null, 403);
        }

        // If all is OK
        return true;
    }

    /**
     * Execute the SharePointListCommand
     * @param SharePointListCommand $command
     * @return \CamelotSoapClient|\CamelotException
     */
    public function execute(SharePointListCommand $command = null) {
        // Set $command as the current instace if $command is null and the instance is set
        if ($command == null && isset($this)) {
            $command = $this;
        }

        // Get the WcfSettingsObject
        $WcfSettingsList = WcfSettingsList::instance();
        $settings = $WcfSettingsList->getSettings($command->connection_name);

        if ($settings instanceof WcfSettingsObject) {

            // Get any cached file
            $download = $this->get_cached_file($settings);

            // If no cached file or if the download resulted in an exception (ensure data connectivity first)
            if (is_null($download) || $download instanceof CamelotException) {
                $client = new CamelotSoapClient($settings);

                // If we have an error, return the client with all exception details
                if ($client->commandStatus === false) {
                    return $client;
                }

                $args = array(
                    'compression' => $settings->compression,
                    'connectionString' => $settings->connection_string,
                    'sharedKey' => $settings->wsdl_shared_key,
                    'listName' => $command->listName,
                    'viewName' => $command->viewName
                );

                try {
                    $download = $client->client->GetListCamelotXml($args)->GetListCamelotXmlResult;

                    // Update the cached file (if required)
                    $cacheResult = $this->set_cached_file($download, $settings);

                    if ($cacheResult instanceof CamelotException) {
                        // TODO, Caching failed
                    }
                } catch (SoapFault $exc) {
                    return new CamelotException($exc);
                }
            }

            if ($settings->compression == 1 || $settings->compression == true || $settings->compression == "true") {
                $download = $this->decode_packet($download);

                if ($download instanceof CamelotException) {
                    return $download;
                }
            }

            $camelotXml = new CamelotXmlReader();
            $packet = $camelotXml->get_list_data($download, 1, $this->columns);
            return $packet;
        } else {
            return new CamelotException(null, 800);
        }
    }

}

?>
